// pages/export/export.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileName: '',
    fileSize: '',
    id: '',
    userInfo: {},
    email: '',
    isOver: true,
    isorg:null,
  },

  emailinput(e) {
    this.setData({
      email: e.detail.value.trim()
    })
  },
  save() {
    var that = this
    var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    if (that.data.email.length == 0) {
      wx.showToast({
        title: "请输入邮箱地址",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!emailPattern.test(that.data.email)) {
      wx.showToast({
        title: "邮箱格式错误",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (that.data.isOver) {
      that.setData({
        isOver: false,
      })
      var url = '';
      var data = {}
      if (that.data.isorg == 1) {
        url = 'orgmini/epidemic/exportEpidemicSituationToMail'
        data = {
          orgId: that.data.id,
          email: that.data.email
        }
      } else if (that.data.isorg == 0) {
        url = 'orgmini/epidemic/exportPersonEpidemicSituationToMail'
        data = {
          userId: wx.getStorageSync('uid'),
          email: that.data.email
        }
      }
      app.req({
        url: url,
        method: "post",
        data:data,
        success: function (res) {
          if (200 == res.code) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
              that.setData({
                isOver: true,
              })
            }, 1000)
            wx.showToast({
              title: '导出成功',
              icon: 'none',
              duration: 2000
            })
          } else {
            that.setData({
              isOver: true,
            })
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  getname(){
    var that = this
    var url=''
    var data={}
    if(that.data.isorg==1){
      url='orgmini/epidemic/exportEpidemicSituationFileInfo'
        data= {
        orgId: that.data.id }
    }else if(that.data.isorg==0){
      url = 'orgmini/epidemic/exportPersonEpidemicSituationFileInfo'
      data = {
        userId: wx.getStorageSync('uid')
      }
    }
    app.req({
      url:url,
      data:data,
      success:function(res){
        if(res.code==200){
          that.setData({
            fileName: res.data.fileName,
            fileSize: res.data.fileSize
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      id: options.orgid,
      isorg:options.isorg
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getname()
    app.getUserInfo(function (userInfo) {
      console.log('用户',userInfo)
      if (userInfo.mail){
        that.setData({
          email: userInfo.mail
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})